<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>高斯——勒让德求积公式与复化高斯型求积公式(等距节点)误差分析 | 洛沐の人间客栈</title><meta name="keywords" content="Python数值分析"><meta name="author" content="洛沐,guojiaxiang0820@gmail.com"><meta name="copyright" content="洛沐"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="本文用Python进行编程实验，分别利用高斯——勒让德求积公式与复化两点高斯型求积公式对积分区间在0-π上被积函数为e^x*cosxdx进行求解，最后进行结果分析与讨论。">
<meta property="og:type" content="article">
<meta property="og:title" content="高斯——勒让德求积公式与复化高斯型求积公式(等距节点)误差分析">
<meta property="og:url" content="https://www.guojxblog.cn/archives/a8ccf4cb.html">
<meta property="og:site_name" content="洛沐の人间客栈">
<meta property="og:description" content="本文用Python进行编程实验，分别利用高斯——勒让德求积公式与复化两点高斯型求积公式对积分区间在0-π上被积函数为e^x*cosxdx进行求解，最后进行结果分析与讨论。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/ComeDate.jpeg">
<meta property="article:published_time" content="2022-05-15T15:59:07.000Z">
<meta property="article:modified_time" content="2023-01-18T14:44:38.784Z">
<meta property="article:author" content="洛沐">
<meta property="article:tag" content="Python数值分析">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/ComeDate.jpeg"><link rel="shortcut icon" href="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/RS_icon.jpg"><link rel="canonical" href="https://www.guojxblog.cn/archives/a8ccf4cb"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="baidu-site-verification" content="code-zGPrfnFp7y"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: {"limitDay":180,"position":"top","messagePrev":"此文中创建于","messageNext":"天之前，请以最新文章为准！"},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":500},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: true
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: 洛沐","link":"链接: ","source":"来源: 洛沐の人间客栈","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'mediumZoom',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '高斯——勒让德求积公式与复化高斯型求积公式(等距节点)误差分析',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-01-18 22:44:38'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const now = new Date()
          const hour = now.getHours()
          const isNight = hour <= 6 || hour >= 18
          if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
          else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/style.css"><link rel="stylesheet" href="/css/iconfont.css"><link rel="stylesheet" href="/css/font-awesome.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/sviptzk/StaticFile_HEXO@latest/butterfly/css/macblack.css"><script src="/live2d-widget/autoload.js"></script><meta name="generator" content="Hexo 6.1.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/touxiang.jpeg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">29</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">18</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fa fa-graduation-cap"></i><span> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fa-solid fa-heart-pulse"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/books/"><i class="fa-fw fas fa-book-reader"></i><span> 读书</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fa fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 影视</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fa-solid fa-keyboard"></i><span> 动态</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言</span></a></li><li><a class="site-page child" href="/notes/"><i class="fa-fw fas fa-feather-alt"></i><span> 随笔</span></a></li><li><a class="site-page child" href="/ideas/"><i class="fa-fw fa-solid fa-lightbulb"></i><span> 妙想</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/ComeDate.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">洛沐の人间客栈</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fa fa-graduation-cap"></i><span> 文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fa-solid fa-heart-pulse"></i><span> 生活</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/books/"><i class="fa-fw fas fa-book-reader"></i><span> 读书</span></a></li><li><a class="site-page child" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fa fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 影视</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="fa-fw fa-solid fa-keyboard"></i><span> 动态</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/comment/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言</span></a></li><li><a class="site-page child" href="/notes/"><i class="fa-fw fas fa-feather-alt"></i><span> 随笔</span></a></li><li><a class="site-page child" href="/ideas/"><i class="fa-fw fa-solid fa-lightbulb"></i><span> 妙想</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">高斯——勒让德求积公式与复化高斯型求积公式(等距节点)误差分析</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-05-15T15:59:07.000Z" title="发表于 2022-05-15 23:59:07">2022-05-15</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-01-18T14:44:38.784Z" title="更新于 2023-01-18 22:44:38">2023-01-18</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%A8%8B%E5%BA%8F%E4%BB%A3%E7%A0%81/">程序代码</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>10分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="高斯——勒让德求积公式与复化高斯型求积公式(等距节点)误差分析"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="实验内容"><a href="#实验内容" class="headerlink" title="实验内容"></a>实验内容</h1><p>试建立8个节点的Gauss-Legendre求积公式，要求数值求积公式中的Gauss点以及求积系数都具有8位有效数字；用所建立的Gauss型求积公式计算积分</p>
<p><img src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/Python%E6%95%B0%E5%80%BC%E5%88%86%E6%9E%90%E4%B9%8B%E9%AB%98%E6%96%AF%E5%8B%92%E8%AE%A9%E5%BE%B7%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E4%B8%8E%E5%A4%8D%E5%8C%96%E9%AB%98%E6%96%AF%E5%9E%8B%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E7%AD%89%E8%B7%9D%E8%8A%82%E7%82%B9/%E7%A7%AF%E5%88%86%E5%85%AC%E5%BC%8F1.png"></p>
<p>的近似值，并用四等分区间的复化两点Gauss型求积公式计算该积分的近似值，对二者的精度进行比较，并解释其原因。（提示：用非线性方程迭代解法求得Gauss 点，求积系数根据插值型求积公式的系数计算，具体的积分值可以用数值方法求得)。</p>
<h1 id="算法原理"><a href="#算法原理" class="headerlink" title="算法原理"></a>算法原理</h1><h2 id="高斯——勒让德求积公式"><a href="#高斯——勒让德求积公式" class="headerlink" title="高斯——勒让德求积公式"></a>高斯——勒让德求积公式</h2><p>插值型求积公式任意给定n + 1个节点，至少有 n 次代数精度。 牛顿-柯特斯公式是等距节点的插值求积公式，当 n 为 偶数时，有n + 1次代数精度。插值求积公式中有n + 1个节点，n + 1个求积系数，共有2n + 2个未知参数，适当选取这些参数可使求积公式具有2n + 1次代数精度，这时就构成了高斯型求积公式。高斯型求积公式就是最高次代数精度的求积公式。高斯公式不局限与等步长分区域，意思是不用二分的方法直接划分区域，在简单的计算下就可以得到高精度的积分式子。插值型求积公式的节点a &lt; x0 &lt; x1 &lt; … &lt; xn &lt; b是高斯点的充分必要条件是以这些节点为零点的多项式。与任何次数不超过n的多项式P（x）带权正交，同时由于其系数都大于0，所以我们也可以得到该求积公式是收敛的。对于勒让德求积公式，就是说我们取权函数P（x）&#x3D;1，同时区间为[-1, 1]，所以勒让德多项式的零点就是我们的求积的高斯点。使插值求积公式有2n+1次代数精度的节点xi,i&#x3D;0,1,…,n称为高斯点，该插值求积公式称为高斯型求积公式，简称高斯公式。高斯点的确定原则上可以根据它们满足的代数精度要求、通过求解非线性方程组得出。对勒让德多项式取其零点作为求积节点，即可构造出高斯-勒让德(Gauss-Legendre)公式，也简称高斯公式。</p>
<h2 id="复化高斯公式"><a href="#复化高斯公式" class="headerlink" title="复化高斯公式"></a>复化高斯公式</h2><p>将求积区间[a，b]分为 n 等分：n&#x3D;(b-a)&#x2F;h上使用低阶高斯公式(如一阶中矩形公式):<br><img src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/Python%E6%95%B0%E5%80%BC%E5%88%86%E6%9E%90%E4%B9%8B%E9%AB%98%E6%96%AF%E5%8B%92%E8%AE%A9%E5%BE%B7%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E4%B8%8E%E5%A4%8D%E5%8C%96%E9%AB%98%E6%96%AF%E5%9E%8B%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E7%AD%89%E8%B7%9D%E8%8A%82%E7%82%B9/%E5%A4%8D%E5%8C%96%E4%B8%A4%E7%82%B9%E9%AB%98%E6%96%AF%E5%9E%8B%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F.png" alt="复化两点高斯型求积公式"><br>高斯公式的值随节点数目的增加而收敛到准确积分值。</p>
<blockquote>
<p>优点:</p>
</blockquote>
<ul>
<li><p>收敛、稳定；</p>
</li>
<li><p>计算量小，代数精度高。</p>
</li>
</ul>
<blockquote>
<p> 缺点：</p>
</blockquote>
<ul>
<li><p>高斯点难求（即多项式的根难求）；</p>
</li>
<li><p>高斯点是无理数，求积系数也是无理数。</p>
</li>
</ul>
<blockquote>
<p> 使用情况：</p>
</blockquote>
<ul>
<li><p>f(x) 赋值量大；</p>
</li>
<li><p>计算的积分多。</p>
</li>
</ul>
<h1 id="程序框图"><a href="#程序框图" class="headerlink" title="程序框图"></a>程序框图</h1><p><img src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/Python%E6%95%B0%E5%80%BC%E5%88%86%E6%9E%90%E4%B9%8B%E9%AB%98%E6%96%AF%E5%8B%92%E8%AE%A9%E5%BE%B7%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E4%B8%8E%E5%A4%8D%E5%8C%96%E9%AB%98%E6%96%AF%E5%9E%8B%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E7%AD%89%E8%B7%9D%E8%8A%82%E7%82%B9/gausslegendre.png" alt="高斯——勒让德求积公式"><br><img src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/Python%E6%95%B0%E5%80%BC%E5%88%86%E6%9E%90%E4%B9%8B%E9%AB%98%E6%96%AF%E5%8B%92%E8%AE%A9%E5%BE%B7%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E4%B8%8E%E5%A4%8D%E5%8C%96%E9%AB%98%E6%96%AF%E5%9E%8B%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E7%AD%89%E8%B7%9D%E8%8A%82%E7%82%B9/compositegausspro.png" alt="复化高斯型求积公式"></p>
<h1 id="编程实现"><a href="#编程实现" class="headerlink" title="编程实现"></a>编程实现</h1><p>本次编程采用Python的面向对象方法进行编写，其中对算法的初步理解以及求解方程的根的数值方法是本程序的难点所在。<br>利用所编程序，确定并计算题目中所给积分的数值解:<br>以下将用Python进行编程实验，分别利用高斯——勒让德求积公式与复化两点高斯型求积公式对积分要求积分进行求解，最后进行结果分析与讨论。<br>Python代码详解及运行结果如下：</p>
<h2 id="代码详解："><a href="#代码详解：" class="headerlink" title="代码详解："></a>代码详解：</h2><blockquote>
<p>gauss_legendre_int.py: 高斯——勒让德求积公式文件。</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time    : 2022/5/13 4:05 下午</span></span><br><span class="line"><span class="comment"># @Author  : Leo</span></span><br><span class="line"><span class="comment"># @FileName: gauss_legendre_int.py</span></span><br><span class="line"><span class="comment"># @Software: PyCharm</span></span><br><span class="line"><span class="comment"># @Blog    ：https://guojxblog.cn</span></span><br><span class="line"><span class="comment"># @GitHub  ：https://github.com/guojx0820</span></span><br><span class="line"><span class="comment"># @Email   ：guojiaxiang0820@gmail.com</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> sympy</span><br><span class="line"><span class="keyword">import</span> math</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">GaussLegendreIntegration</span>:</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    高斯——勒让德求积公式，核心部分求解零点与系数</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, int_fun, int_interval, zeros_num=<span class="number">10</span></span>):</span><br><span class="line">        self.int_fun = int_fun  <span class="comment"># 被积函数，符号定义</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">len</span>(int_interval) == <span class="number">2</span>:</span><br><span class="line">            self.a, self.b = int_interval[<span class="number">0</span>], int_interval[<span class="number">1</span>]  <span class="comment"># 被积区间</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">raise</span> ValueError(<span class="string">&quot;积分区间参数设置不规范，应为[a,b].&quot;</span>)</span><br><span class="line">        self.n = <span class="built_in">int</span>(zeros_num)  <span class="comment"># 正交多项式的零点数</span></span><br><span class="line">        self.zeros_points = <span class="literal">None</span>  <span class="comment"># 勒让德高斯零点</span></span><br><span class="line">        self.int_value = <span class="literal">None</span>  <span class="comment"># 积分值结果</span></span><br><span class="line">        self.A_k = <span class="literal">None</span>  <span class="comment"># 求积系数</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">cal_int</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        高斯——勒让德求积公式</span></span><br><span class="line"><span class="string">        :return:</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        self._cal_Ak_coef_()  <span class="comment"># 求解求积系数Ak与零点</span></span><br><span class="line">        fun_val = self.int_fun(self.zeros_points)  <span class="comment"># 零点函数值</span></span><br><span class="line">        self.int_value = np.dot(self.A_k, fun_val)  <span class="comment"># 插值型求积公式</span></span><br><span class="line">        <span class="keyword">return</span> self.int_value</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">_cal_gauss_zeros_points_</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        计算高斯零点</span></span><br><span class="line"><span class="string">        :return:</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        t = sympy.Symbol(<span class="string">&#x27;t&#x27;</span>)</span><br><span class="line">        <span class="comment"># 勒让德多项式构造</span></span><br><span class="line">        p_n = (t ** <span class="number">2</span> - <span class="number">1</span>) ** self.n / (math.factorial(self.n) * <span class="number">2</span> ** self.n)</span><br><span class="line">        diff_p_n = sympy.diff(p_n, t, self.n)  <span class="comment"># 多项式的n阶导数</span></span><br><span class="line">        <span class="comment"># print(diff_p_n)</span></span><br><span class="line">        <span class="comment"># 求解高斯——勒让德多项式的全部零点</span></span><br><span class="line">        self.zeros_points = np.asarray(sympy.solve(diff_p_n, t), dtype=np.float_)</span><br><span class="line">        <span class="comment"># print(&quot;高斯点:&quot;, self.zeros_points, sep=&quot;\n&quot;)</span></span><br><span class="line">        <span class="keyword">return</span> diff_p_n, t</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">_cal_Ak_coef_</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        计算Ak系数</span></span><br><span class="line"><span class="string">        :return:</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        diff_p_n, t = self._cal_gauss_zeros_points_()  <span class="comment"># 求解高斯零点，传递函数和符号参数</span></span><br><span class="line">        Ak_poly = sympy.lambdify(t, <span class="number">2</span> / ((<span class="number">1</span> - t ** <span class="number">2</span>) * (diff_p_n.diff(t, <span class="number">1</span>) ** <span class="number">2</span>)))</span><br><span class="line">        self.A_k = Ak_poly(self.zeros_points)  <span class="comment"># 求解求积系数Ak</span></span><br><span class="line">        <span class="comment"># 区间转换，[a, b] --&gt; [-1, 1]</span></span><br><span class="line">        self.A_k = self.A_k * (self.b - self.a) / <span class="number">2</span></span><br><span class="line">        self.zeros_points = (self.b - self.a) / <span class="number">2</span> * self.zeros_points + (self.a + self.b) / <span class="number">2</span></span><br><span class="line">        <span class="comment"># print(&quot;高斯点:&quot;, self.zeros_points, sep=&quot;\n&quot;)</span></span><br><span class="line">        <span class="comment"># print(&quot;求积系数:&quot;, self.A_k, sep=&quot;\n&quot;)</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>test_gauss_legendre.py: 高斯——勒让德求积公式测试文件。</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time    : 2022/5/13 4:57 下午</span></span><br><span class="line"><span class="comment"># @Author  : Leo</span></span><br><span class="line"><span class="comment"># @FileName: test_gauss_legendre.py</span></span><br><span class="line"><span class="comment"># @Software: PyCharm</span></span><br><span class="line"><span class="comment"># @Blog    ：https://guojxblog.cn</span></span><br><span class="line"><span class="comment"># @GitHub  ：https://github.com/guojx0820</span></span><br><span class="line"><span class="comment"># @Email   ：guojiaxiang0820@gmail.com</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> sympy</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="keyword">from</span> NumericalIntegration.gauss_legendre_int <span class="keyword">import</span> GaussLegendreIntegration</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">fun</span>(<span class="params">x</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    定义积分的被积函数</span></span><br><span class="line"><span class="string">    :param x:自变量</span></span><br><span class="line"><span class="string">    :return:被积函数</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">return</span> np.cos(x) * np.exp(x)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    gauss_zeros_num = np.arange(<span class="number">8</span>, <span class="number">20</span>, <span class="number">1</span>)</span><br><span class="line">    int_accurate = -<span class="number">0.5</span> * (np.exp(np.pi) + <span class="number">1</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;精确值:&quot;</span>, int_accurate)</span><br><span class="line">    precision = []</span><br><span class="line">    <span class="keyword">for</span> num <span class="keyword">in</span> gauss_zeros_num:</span><br><span class="line">        legendre = GaussLegendreIntegration(fun, [<span class="number">0</span>, np.pi], zeros_num=num)</span><br><span class="line">        int_value = legendre.cal_int()</span><br><span class="line">        precision.append(int_accurate - int_value)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;num:%d,积分值:%.15f,误差:%.15e&quot;</span> % (num, int_value, precision[-<span class="number">1</span>]))  <span class="comment"># append list中最后一个值用precision[-1]</span></span><br><span class="line">    plt.figure(figsize=(<span class="number">8</span>, <span class="number">6</span>))</span><br><span class="line">    plt.title(<span class="string">&quot;Gauss-Legendre Quadrature Formula Error Curve&quot;</span>, fontdict=&#123;<span class="string">&quot;fontsize&quot;</span>: <span class="number">15</span>&#125;)</span><br><span class="line">    plt.xlabel(<span class="string">&quot;The Number of Gauss Zero-Points&quot;</span>, fontdict=&#123;<span class="string">&quot;fontsize&quot;</span>: <span class="number">12</span>&#125;)</span><br><span class="line">    plt.ylabel(<span class="string">&quot;Error&quot;</span>, fontdict=&#123;<span class="string">&quot;fontsize&quot;</span>: <span class="number">12</span>&#125;)</span><br><span class="line">    plt.plot(gauss_zeros_num, precision, <span class="string">&#x27;ro-&#x27;</span>)</span><br><span class="line">    plt.grid(axis=<span class="string">&#x27;y&#x27;</span>, color=<span class="string">&#x27;b&#x27;</span>, linestyle=<span class="string">&#x27;--&#x27;</span>, linewidth=<span class="number">0.5</span>)</span><br><span class="line">    plt.savefig(<span class="string">&quot;/Users/leo/Desktop/Gauss-Legendre.png&quot;</span>, dpi=<span class="number">300</span>, bbox_inches=<span class="string">&quot;tight&quot;</span>)</span><br><span class="line">    plt.show()</span><br><span class="line">    <span class="comment"># legendre._cal_gauss_zeros_points_()</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>composite_2points_gauss_legendre.py:复化两点高斯型求积公式文件。</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time    : 2022/5/15 5:11 下午</span></span><br><span class="line"><span class="comment"># @Author  : Leo</span></span><br><span class="line"><span class="comment"># @FileName: composite_2points_gauss_legendre.py</span></span><br><span class="line"><span class="comment"># @Software: PyCharm</span></span><br><span class="line"><span class="comment"># @Blog    ：https://guojxblog.cn</span></span><br><span class="line"><span class="comment"># @GitHub  ：https://github.com/guojx0820</span></span><br><span class="line"><span class="comment"># @Email   ：guojiaxiang0820@gmail.com</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">CompositeGaussLegendreIntegration</span>:</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    复化两点高斯——勒让德求积公式，等距节点数自定义</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, int_fun, int_interval, interval_num=<span class="number">4</span></span>):</span><br><span class="line">        self.int_fun = int_fun  <span class="comment"># 被积函数，符号定义</span></span><br><span class="line">        <span class="keyword">if</span> <span class="built_in">len</span>(int_interval) == <span class="number">2</span>:</span><br><span class="line">            self.a, self.b = int_interval[<span class="number">0</span>], int_interval[<span class="number">1</span>]  <span class="comment"># 被积区间</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">raise</span> ValueError(<span class="string">&quot;积分区间参数设置不规范，应为[a, b].&quot;</span>)</span><br><span class="line">        self.interval_num = interval_num</span><br><span class="line">        self.int_value = <span class="literal">None</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">cal_int</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        复化两点高斯——勒让德求积公式</span></span><br><span class="line"><span class="string">        :return:</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        fun_value = <span class="number">0</span>  <span class="comment"># 初始化函数值</span></span><br><span class="line">        interval_len = (self.b - self.a) / self.interval_num</span><br><span class="line">        <span class="keyword">for</span> k <span class="keyword">in</span> <span class="built_in">range</span>(self.interval_num - <span class="number">1</span>):</span><br><span class="line">            fun_value += self.int_fun(self.a + (k + <span class="number">1</span> / <span class="number">2</span>) * interval_len)</span><br><span class="line">        self.int_value = interval_len * fun_value</span><br><span class="line">        <span class="keyword">return</span> self.int_value</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<blockquote>
<p>test_composite_2points_gauss_legendre.py:复化两点高斯型求积公式测试文件。</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time    : 2022/5/15 5:56 下午</span></span><br><span class="line"><span class="comment"># @Author  : Leo</span></span><br><span class="line"><span class="comment"># @FileName: test_composite_2points_gauss_legendre.py</span></span><br><span class="line"><span class="comment"># @Software: PyCharm</span></span><br><span class="line"><span class="comment"># @Blog    ：https://guojxblog.cn</span></span><br><span class="line"><span class="comment"># @GitHub  ：https://github.com/guojx0820</span></span><br><span class="line"><span class="comment"># @Email   ：guojiaxiang0820@gmail.com</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="keyword">import</span> symbol</span><br><span class="line"><span class="keyword">from</span> NumericalIntegration.composite_2points_gauss_legendre <span class="keyword">import</span> CompositeGaussLegendreIntegration</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">fun</span>(<span class="params">x</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    定义积分被积函数</span></span><br><span class="line"><span class="string">    :param x: 自由变量</span></span><br><span class="line"><span class="string">    :return:</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">return</span> np.cos(x) * np.exp(x)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    interval_num = np.arange(<span class="number">100000</span>, <span class="number">1000000</span>, <span class="number">100000</span>)</span><br><span class="line">    int_accurate = -<span class="number">0.5</span> * (np.exp(np.pi) + <span class="number">1</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;精确解:&quot;</span>, int_accurate)</span><br><span class="line">    precision = []</span><br><span class="line">    <span class="keyword">for</span> num <span class="keyword">in</span> interval_num:</span><br><span class="line">        legendre = CompositeGaussLegendreIntegration(fun, [<span class="number">0</span>, np.pi], interval_num=num)</span><br><span class="line">        int_value = legendre.cal_int()</span><br><span class="line">        precision.append(int_accurate - int_value)</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;num=%d,积分值:%.15f,误差:%.15e&quot;</span> % (num, int_value, precision[-<span class="number">1</span>]))  <span class="comment"># append list中最后一个值用precision[-1]</span></span><br><span class="line">    plt.figure(figsize=(<span class="number">8</span>, <span class="number">6</span>))</span><br><span class="line">    plt.title(<span class="string">&quot;Composite Gauss Quadrature Formula Error Curve&quot;</span>, fontdict=&#123;<span class="string">&quot;fontsize&quot;</span>: <span class="number">16</span>&#125;)</span><br><span class="line">    plt.xlabel(<span class="string">&quot;The Number of Integral Intervals&quot;</span>, fontdict=&#123;<span class="string">&quot;fontsize&quot;</span>: <span class="number">12</span>&#125;)</span><br><span class="line">    plt.ylabel(<span class="string">&quot;Error&quot;</span>, fontdict=&#123;<span class="string">&quot;fontsize&quot;</span>: <span class="number">12</span>&#125;)</span><br><span class="line">    plt.plot(interval_num, precision, <span class="string">&#x27;ro-&#x27;</span>)</span><br><span class="line">    plt.grid(axis=<span class="string">&#x27;y&#x27;</span>, color=<span class="string">&#x27;b&#x27;</span>, linestyle=<span class="string">&#x27;--&#x27;</span>, linewidth=<span class="number">0.5</span>)</span><br><span class="line">    plt.savefig(<span class="string">&quot;/Users/leo/Desktop/CompositeGauss.png&quot;</span>, dpi=<span class="number">300</span>, bbox_inches=<span class="string">&quot;tight&quot;</span>)</span><br><span class="line">    plt.show()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="运行结果"><a href="#运行结果" class="headerlink" title="运行结果"></a>运行结果</h2><p><img src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/Python%E6%95%B0%E5%80%BC%E5%88%86%E6%9E%90%E4%B9%8B%E9%AB%98%E6%96%AF%E5%8B%92%E8%AE%A9%E5%BE%B7%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E4%B8%8E%E5%A4%8D%E5%8C%96%E9%AB%98%E6%96%AF%E5%9E%8B%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E7%AD%89%E8%B7%9D%E8%8A%82%E7%82%B9/%E9%AB%98%E6%96%AF%E7%82%B9.png" alt="高斯——勒让德求积公式高斯点计算结果"></p>
<p><img src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/Python%E6%95%B0%E5%80%BC%E5%88%86%E6%9E%90%E4%B9%8B%E9%AB%98%E6%96%AF%E5%8B%92%E8%AE%A9%E5%BE%B7%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E4%B8%8E%E5%A4%8D%E5%8C%96%E9%AB%98%E6%96%AF%E5%9E%8B%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E7%AD%89%E8%B7%9D%E8%8A%82%E7%82%B9/%E6%B1%82%E7%A7%AF%E7%B3%BB%E6%95%B0.png" alt="高斯——勒让德求积公式求积系数计算结果"></p>
<p><img src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/Python%E6%95%B0%E5%80%BC%E5%88%86%E6%9E%90%E4%B9%8B%E9%AB%98%E6%96%AF%E5%8B%92%E8%AE%A9%E5%BE%B7%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E4%B8%8E%E5%A4%8D%E5%8C%96%E9%AB%98%E6%96%AF%E5%9E%8B%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E7%AD%89%E8%B7%9D%E8%8A%82%E7%82%B9/%E6%95%B0%E5%80%BC%E8%A7%A3%E5%8F%8A%E8%AF%AF%E5%B7%AE.png" alt="高斯——勒让德求积公式精确值与数值解及误差"></p>
<p><img src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/Python%E6%95%B0%E5%80%BC%E5%88%86%E6%9E%90%E4%B9%8B%E9%AB%98%E6%96%AF%E5%8B%92%E8%AE%A9%E5%BE%B7%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E4%B8%8E%E5%A4%8D%E5%8C%96%E9%AB%98%E6%96%AF%E5%9E%8B%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E7%AD%89%E8%B7%9D%E8%8A%82%E7%82%B9/Gauss-Legendre.png" alt="高斯——勒让德求积公式误差曲线"></p>
<p><img src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/Python%E6%95%B0%E5%80%BC%E5%88%86%E6%9E%90%E4%B9%8B%E9%AB%98%E6%96%AF%E5%8B%92%E8%AE%A9%E5%BE%B7%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E4%B8%8E%E5%A4%8D%E5%8C%96%E9%AB%98%E6%96%AF%E5%9E%8B%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E7%AD%89%E8%B7%9D%E8%8A%82%E7%82%B9/%E6%95%B0%E5%80%BC%E8%A7%A3%E5%8F%8A%E8%AF%AF%E5%B7%AE1.png" alt="复化两点高斯求积公式精确值与数值解及误差"></p>
<p><img src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/Python%E6%95%B0%E5%80%BC%E5%88%86%E6%9E%90%E4%B9%8B%E9%AB%98%E6%96%AF%E5%8B%92%E8%AE%A9%E5%BE%B7%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E4%B8%8E%E5%A4%8D%E5%8C%96%E9%AB%98%E6%96%AF%E5%9E%8B%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F%E7%AD%89%E8%B7%9D%E8%8A%82%E7%82%B9/CompositeGauss.png" alt="复化高斯型求积公式误差曲线"></p>
<h1 id="结果分析与讨论"><a href="#结果分析与讨论" class="headerlink" title="结果分析与讨论"></a>结果分析与讨论</h1><ul>
<li><p>有两种求积公式的结果可以看出，高斯——勒让德求积公式计算较为复杂，但收敛速度快且精度较高，在20个高斯零点以内可以收敛，且误差缩小到1e-14的个数量级；而复化两点高斯型积分公式收敛速度较慢，需要将区间分为1e6个小区间之后，精度只能达到1e-6个数量级，继续增加其区间，缩小步长时，程序运行速度明显变慢，说明其效率低于高斯——勒让德求积公式（由于n&#x3D;4，四等分区间时，其结果差异很大，所以舍弃，只能用百万区间划分才能确保精度）。</p>
</li>
<li><p>高斯——勒让德求积公式精度高，收敛速度快的主要原因是：高斯——勒让德求积公式的高斯节点的选取的特殊性，并不采用等距节点，而是利用勒让德多项式的插值节点作为高斯点进行积分，其结果更加精确，且收敛速度更快，也更加容易接近精确解。</p>
</li>
<li><p>反观等距节点插值，对于高次具有强震荡曲线的函数，效果并不理想。主要是由于函数在小区间内的变化率太大，所引起的误差较大，因此需要不断增加其等距节点个数，缩小步长或区间范围，在小区间中用高斯型求积公式进行积分求解才更有效果。</p>
</li>
</ul>
<h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1><p><strong>[1]《数值分析》欧阳洁等。</strong></p>
<p><strong>[2]</strong> <a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.bilibili.com/video/BV1Lq4y1U7Hj?p=29&amp;spm_id_from=pageDriver">https://www.bilibili.com/video/BV1Lq4y1U7Hj?p=29&amp;spm_id_from=pageDriver</a></p>
<p><strong>[3]</strong> <a target="_blank" rel="noopener external nofollow noreferrer" href="https://wenku.baidu.com/view/b3fc62d74b7302768e9951e79b89680203d86bb7.html">https://wenku.baidu.com/view/b3fc62d74b7302768e9951e79b89680203d86bb7.html</a></p>
<p><strong>[4]</strong> <a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/poqw2010/article/details/79487919">https://blog.csdn.net/poqw2010/article/details/79487919</a></p>
<p><strong>[5]</strong> <a target="_blank" rel="noopener external nofollow noreferrer" href="https://blog.csdn.net/catinlbb/article/details/103534439">https://blog.csdn.net/catinlbb/article/details/103534439</a></p>
<p><strong>[6]</strong> <a target="_blank" rel="noopener external nofollow noreferrer" href="https://www.doc88.com/p-3901680450501.html">https://www.doc88.com/p-3901680450501.html</a></p>
<p><strong>[7]</strong> <a target="_blank" rel="noopener external nofollow noreferrer" href="https://zhuanlan.zhihu.com/p/439768015">https://zhuanlan.zhihu.com/p/439768015</a></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:guojiaxiang0820@gmail.com" rel="external nofollow noreferrer">洛沐</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://www.guojxblog.cn/archives/a8ccf4cb.html">https://www.guojxblog.cn/archives/a8ccf4cb.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noreferrer" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://www.guojxblog.cn" target="_blank">洛沐の人间客栈</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Python%E6%95%B0%E5%80%BC%E5%88%86%E6%9E%90/">Python数值分析</a></div><div class="post_share"><div class="social-share" data-image="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/ComeDate.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/wechat.jpeg" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/wechat.jpeg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/alipay.jpeg" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/alipay.jpeg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/archives/49977e84.html"><img class="prev-cover" src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/beauty_top.jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">惊人的美</div></div></a></div><div class="next-post pull-right"><a href="/archives/8063fed6.html"><img class="next-cover" src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/Python%E6%95%B0%E5%AD%97%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86%E5%9F%BA%E7%A1%80%E4%B9%8BOpenCV%E5%9B%BE%E5%83%8F%E8%AF%BB%E5%8F%96%E4%B8%8E%E6%98%BE%E7%A4%BA/hefu.jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">OpenCV图像读取与显示</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/archives/330ac4a4.html" title="四阶Runge-Kutta公式与Adams预测校正公式求解常微分方程初值问题"><img class="cover" src="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/loading3.gif" data-original="https://luomublog.oss-cn-qingdao.aliyuncs.com/ImgHost/%E5%9B%9B%E9%98%B6Runge-Kutta%E5%85%AC%E5%BC%8F%E4%B8%8EAdams%E9%A2%84%E6%B5%8B%E6%A0%A1%E6%AD%A3%E5%85%AC%E5%BC%8F%E6%B1%82%E8%A7%A3%E5%B8%B8%E5%BE%AE%E5%88%86%E6%96%B9%E7%A8%8B%E5%88%9D%E5%80%BC%E9%97%AE%E9%A2%98/bubble.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-01</div><div class="title">四阶Runge-Kutta公式与Adams预测校正公式求解常微分方程初值问题</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="lv-container" data-id="city" data-uid="MTAyMC81NjIzOS8zMjcwMg=="></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E5%86%85%E5%AE%B9"><span class="toc-number">1.</span> <span class="toc-text">实验内容</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AE%97%E6%B3%95%E5%8E%9F%E7%90%86"><span class="toc-number">2.</span> <span class="toc-text">算法原理</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%AB%98%E6%96%AF%E2%80%94%E2%80%94%E5%8B%92%E8%AE%A9%E5%BE%B7%E6%B1%82%E7%A7%AF%E5%85%AC%E5%BC%8F"><span class="toc-number">2.1.</span> <span class="toc-text">高斯——勒让德求积公式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%8D%E5%8C%96%E9%AB%98%E6%96%AF%E5%85%AC%E5%BC%8F"><span class="toc-number">2.2.</span> <span class="toc-text">复化高斯公式</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%A8%8B%E5%BA%8F%E6%A1%86%E5%9B%BE"><span class="toc-number">3.</span> <span class="toc-text">程序框图</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BC%96%E7%A8%8B%E5%AE%9E%E7%8E%B0"><span class="toc-number">4.</span> <span class="toc-text">编程实现</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E8%AF%A6%E8%A7%A3%EF%BC%9A"><span class="toc-number">4.1.</span> <span class="toc-text">代码详解：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C"><span class="toc-number">4.2.</span> <span class="toc-text">运行结果</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BB%93%E6%9E%9C%E5%88%86%E6%9E%90%E4%B8%8E%E8%AE%A8%E8%AE%BA"><span class="toc-number">5.</span> <span class="toc-text">结果分析与讨论</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8F%82%E8%80%83"><span class="toc-number">6.</span> <span class="toc-text">参考</span></a></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2023  <i id="heartbeat" class="fa fas fa-heartbeat"></i> 洛沐</div><div class="footer_custom_text">谢谢你来看<a href="https://www.guojxblog.cn/" style='color:red;Font-size:36'>我</a>，你今天真好看😘</div></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/HCLonely/images@master/others/heartbeat.min.css"></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">簡</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>function loadLivere () {
  if (typeof LivereTower === 'object') {
    window.LivereTower.init()
  }
  else {
    (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
    })(document, 'script');
  }
}

if ('Livere' === 'Livere' || !true) {
  if (true) btf.loadComment(document.getElementById('lv-container'), loadLivere)
  else loadLivere()
}
else {
  function loadOtherComment () {
    loadLivere()
  }
}</script></div><script src="/js/mobile_side.js"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="true"></script><div class="pjax-reload"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '700ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
}</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
}</script></div><script defer="defer" src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer="defer" src="/js/wow_init.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script>
            window.imageLazyLoadSetting = {
                isSPA: true,
                preloadRatio: 3,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(n){n.imageLazyLoadSetting.processImages=o;var e=n.imageLazyLoadSetting.isSPA,i=n.imageLazyLoadSetting.preloadRatio||1,r=Array.prototype.slice.call(document.querySelectorAll("img[data-original]"));function o(){e&&(r=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")));for(var t,a=0;a<r.length;a++)0<=(t=(t=r[a]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(n.innerHeight*i||document.documentElement.clientHeight*i)&&function(){var t,e,n,i,o=r[a];t=o,e=function(){r=r.filter(function(t){return o!==t})},n=new Image,i=t.getAttribute("data-original"),n.onload=function(){t.src=i,e&&e()},t.src!==i&&(n.src=i)}()}o(),n.addEventListener("scroll",function(){var t,e;t=o,e=n,clearTimeout(t.tId),t.tId=setTimeout(function(){t.call(e)},500)})}(this);</script><script async>window.onload=function(){var a=document.createElement('script'),b=document.getElementsByTagName('script')[0];a.type='text/javascript',a.async=!0,a.src='/sw-register.js?v='+Date.now(),b.parentNode.insertBefore(a,b)};</script></body></html>